################################################################################
#
#                           Target Definitions
#
#  Description:
#    This makefile structure assumes that the following variables are defined:
#
#      PRIMARY_FILE = name of the target
#      CSRCS = the name of the C sources
#
#      INCLUDEDIRS = list of directories to include (i.e. -I$(ROOT)/...)
#      LIBS = list of libraries needed to build the target (i.e. -lname)
#      LIBDIRS = list of directories to search for libaries (i.e. -L$(ROOT)/...)
#      COMPILE_DEPENDS = list of dependencies for creating the object
#      LINK_DEPENDS = list of dependencies for linking the target
#
#      TARGET = target of the makefile
#
#    To facilitate the process where there are a large number of libraries,
#    rules files are provided in each libary directory that add their
#    contributions to INCLUDEDIRS, LIBS, LIBDIRS, and DEPENDS
#
################################################################################

OBJDIR = OBJ

# Primary file definition
PRIMARY_FILE = simpleServerLib

CSRCS = server.c hwInterfaceServer.c sockets.c
COMPILE_DEPENDS = simpleServerLib.make                                     \
                  ../include/server.h                                      \
                  ../include/tcpServer.h                                   \
                  ../include/tcpServerProtocol.h                           \
                  ../include/tcpServerConfig.h                             \
                  ../include/sockets.h                                     \
                  ../include/sockets_platform.h                            \
                  ../include/hwInterfaceBase.h
INCLUDEDIRS = -I../include
ifeq ($(OSTYPE),cygwin)
  C_USER_FLAGS += -DWIN32
endif

LIB = ../lib/lib$(PRIMARY_FILE).a
.DEFAULT_GOAL := $(LIB)

clean:
	rm -f $(OBJS)
	rm -f $(LIB)

# Define the make rules for the target
# This should be included in a Makefile after the following have been defined:
#
# LIB = libary target
# OBJS = list of .o files (to be made from .c)
# CSRCS = list of source files (.c)
# COMPILE_DEPENDS = list of dependendies for creating the object
# C_USER_FLAGS = optional flags supplied by the user for C compilation
# OBJDIR = target directory in which to place object files
#
# INCLUDEDIRS = additional list of include directories (preceeded with the -I)
# LIBS = additional list of libraries to include (preceeded with the -l)
# LIBDIRS = additional list of library directories to include (preceeded with the -L)

# define the compiler commands and compile flags for C
CC = aarch64-linux-gnu-gcc
#CC = gcc
AR = ar
RANLIB = ranlib
CFLAGS = -O3 -c -static -ansi -pedantic -Wall -fmessage-length=0

# list of all C and objects.  OBJDIR is the target directory where
# .o files will be placed.  If it doesn't exist, OBJDIR wil be created.
OBJS := $(addprefix $(OBJDIR)/,$(CSRCS:.c=.o))

$(OBJS): | $(OBJDIR)
$(OBJDIR):
	mkdir $(OBJDIR)

# Rule for compiling c files
$(OBJDIR)/%.o : %.c $(COMPILE_DEPENDS)
	$(CC) $(CFLAGS) $(C_USER_FLAGS) $(INCLUDEDIRS) $(OUTPUT_OPTION) $<


# Rule for building and installing a library
$(LIB): $(OBJS)
	if [ ! -d $(dir $@) ] ; then mkdir -p $(dir $@) ; fi
	rm -f $(LIB)
	$(AR) cq $(LIB) $(OBJS)
	$(RANLIB) $(LIB)
